package com.imis.module.system.bus;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imis.base.constant.CommonConstant;
import com.imis.base.constant.enums.SysTipEnum;
import com.imis.base.globle.Result;
import com.imis.base.util.ConvertUtils;
import com.imis.base.util.SerialCodeUtil;
import com.imis.module.base.BaseBus;
import com.imis.module.system.model.converter.SysOrganizationConverter;
import com.imis.module.system.model.po.SysOrganization;
import com.imis.module.system.model.po.SysUser;
import com.imis.module.system.model.ro.*;
import com.imis.module.system.model.vo.SysOrganizationTreeVO;
import com.imis.module.system.model.vo.SysOrganizationVO;
import com.imis.module.system.model.vo.SysUserOrganizationVO;
import com.imis.module.system.service.ISysOrganizationService;
import com.imis.module.system.service.ISysUserOrganizationService;
import com.imis.module.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 组织机构 业务处理类
 * </p>
 *
 * @author XinLau
 * @since 2020-04-22
 */
@Slf4j
@Service
public class SysOrganizationBus extends BaseBus {

    /**
     * 组织机构 服务类
     */
    private ISysOrganizationService serviceBySysOrganizationService;

    @Autowired
    public void setISysOrganizationService(ISysOrganizationService serviceBySysOrganizationService) {
        this.serviceBySysOrganizationService = serviceBySysOrganizationService;
    }

    /**
     * 系统用户 服务类
     */
    private ISysUserService serviceBySysUserService;

    @Autowired
    public void setServiceBySysUserService(ISysUserService serviceBySysUserService) {
        this.serviceBySysUserService = serviceBySysUserService;
    }

    /**
     * 用户-组织机构关联表 服务类
     */
    private ISysUserOrganizationService sysUserOrganizationService;

    @Autowired
    public void setSysUserOrganizationService(ISysUserOrganizationService sysUserOrganizationService) {
        this.sysUserOrganizationService = sysUserOrganizationService;
    }

    /**
     * 添加组织机构重复校验
     *
     * @param sysOrganization - 组织机构
     * @return Result
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/9 9:47
     */
    private Result organizationAddVerification(final SysOrganization sysOrganization) {
        if (sysOrganization != null) {
//            1.根据组织机构信息查询，该组织机构被删除
            if (sysOrganization.getDelFlag().equals(CommonConstant.DEL_FLAG_DELETE)) {
                addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ORGANIZATION_ADD_ERR_DELETE.toString(), sysOrganization.getOrganizationName());
                return Result.errorsFormat(SysTipEnum.ORGANIZATION_ADD_ERR_DELETE, sysOrganization.getOrganizationName());
            }
//            2.根据组织机构信息查询，该组织机构名存在
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ORGANIZATION_ADD_ERR_REPEAT.toString(), sysOrganization.getOrganizationName());
            return Result.errorsFormat(SysTipEnum.ORGANIZATION_ADD_ERR_REPEAT, sysOrganization.getOrganizationName());
        }
        return Result.ok();
    }

    /**
     * 添加组织机构父级组织机构校验
     *
     * @param sysOrganizationParent - 父级组织机构
     * @return Result
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/9 9:47
     */
    private Result organizationAddVerificationForParent(final SysOrganization sysOrganizationParent) {
        Result result = new Result<>();
        if (sysOrganizationParent != null) {
//            1.根据父级组织机构信息查询，该父级组织机构被删除
            if (sysOrganizationParent.getDelFlag().equals(CommonConstant.DEL_FLAG_DELETE)) {
                addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ORGANIZATION_ADD_ERR_PARENT_DELETE.toString(), sysOrganizationParent.getOrganizationName());
                return result.errorFormat(SysTipEnum.ORGANIZATION_ADD_ERR_PARENT_DELETE, sysOrganizationParent.getOrganizationName());
            }
            return result;
        }
//        2.根据父级组织机构信息查询，该父级组织机构不存在
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ORGANIZATION_ADD_ERR_PARENT_NON.toString());
        return result.errorFormat(SysTipEnum.ORGANIZATION_ADD_ERR_PARENT_NON);
    }

    /**
     * 生成新的组织机构编号
     *
     * @param parentCode - 父级编码
     * @param parentId - 父级编号
     * @return String[] - 组织机构编号
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/6 17:26
     */
    private String[] generateOrganizationCode(final String parentCode, final Long parentId) {
        // 1.根据父级组织机构编号查询子级组织机构
        List<SysOrganizationVO> sysOrganizationVOList = serviceBySysOrganizationService.selectSysOrganizationListByParentId(parentId == null ? 0 : parentId);
        String maxOrganizationCode = CommonConstant.EMPTY;
        // 2.1存在同级别组织机构
        if (sysOrganizationVOList.size() > 0) {
            maxOrganizationCode = sysOrganizationVOList.get(0).getOrganizationCode();
            for (int i = 0; i < sysOrganizationVOList.size(); i++) {
                Integer maxCode = Integer.valueOf(maxOrganizationCode.substring(maxOrganizationCode.length() - 2));
                String organizationCode = sysOrganizationVOList.get(i).getOrganizationCode();
                Integer code = Integer.valueOf(organizationCode.substring(organizationCode.length() - 2));
                if (code > maxCode) {
                    // 对比最值，确定最值
                    maxOrganizationCode = organizationCode;
                }
            }
        }
        // 生成   organizationCod
        String organizationCod = SerialCodeUtil.getSubSerialCode(parentCode, maxOrganizationCode);
        // 解析 organizationCod
        return SerialCodeUtil.cuttingSerialCode(organizationCod);
    }

    /**
     * 删除组织机构校验
     *
     * @param sysOrganization - 组织机构对象
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    private Result organizationDeleteVerification(final SysOrganization sysOrganization) {
        Result result = new Result<>();
        if (ConvertUtils.isEmpty(sysOrganization)) {
//            1.根据组织机构信息查询，该组织机构不存在
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.ORGANIZATION_DELETE_ERR_NON.toString());
            return result.errorFormat(SysTipEnum.MENU_DELETE_ERR_NON);
        }
//        1.判断组织机构是否分配
        Long[] sysUserIdArray = serviceBySysOrganizationService.queryUserIdentificationByOrganizationIdentification(sysOrganization.getId());
        if (sysUserIdArray.length > 0){
//            根据组织机构信息查询，该组织机构已分配给人员使用
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.ORGANIZATION_DELETE_ERR_ASSIGNED.toString(), sysOrganization.getOrganizationName(), Arrays.toString(sysUserIdArray));
            return result.errorFormat(SysTipEnum.ORGANIZATION_DELETE_ERR_ASSIGNED, sysOrganization.getOrganizationName(), Arrays.toString(sysUserIdArray));
        }
//        2.判断组织机构旗下是否存在子级组织机构
        List<SysOrganizationVO> sysMenuChildList = serviceBySysOrganizationService.selectSysOrganizationListByParentId(sysOrganization.getId());
        if (sysMenuChildList.size() != 0) {
//            3.根据组织机构信息查询，该组织机构旗下存在子级组织机构
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.ORGANIZATION_DELETE_ERR_HAVE_CHILD.toString(), sysOrganization.getOrganizationName());
            return result.errorFormat(SysTipEnum.ORGANIZATION_DELETE_ERR_HAVE_CHILD, sysOrganization.getOrganizationName());
        }
        result.setResult(SysOrganizationConverter.INSTANCE.po2Vo(sysOrganization));
        return result;
    }

    /**
     * 组织机构更新校验
     *
     * @param sysOrganization - 组织机构更新对象
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    private Result organizationUpdateVerification(final SysOrganization sysOrganization) {
        Result result = new Result<>();
        if (ConvertUtils.isEmpty(sysOrganization)) {
//            1.根据组织机构信息查询，该组织机构不存在
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ORGANIZATION_UPDATE_ERR_NON.toString(), sysOrganization.getOrganizationName());
            return result.errorFormat(SysTipEnum.ORGANIZATION_UPDATE_ERR_NON, sysOrganization.getOrganizationName());
        }
        result.setResult(SysOrganizationConverter.INSTANCE.po2Vo(sysOrganization));
        return result;
    }

    /**
     * 用户授权组织机构校验
     *
     * @param sysGrantOrganizationToUserRO - 用户授权组织机构
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    private Result grantOrganizationToUserVerification(final SysGrantOrganizationToUserRO sysGrantOrganizationToUserRO) {
//        1.验证用户合法性
        Long userId = sysGrantOrganizationToUserRO.getUserId();
        SysUser sysUser = serviceBySysUserService.getById(userId);
        if (ConvertUtils.isEmpty(sysUser)) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.GRANT_ORGANIZATION_TO_USER_ERR_NON_USER.toString(), userId);
            return Result.errorsFormat(SysTipEnum.GRANT_ORGANIZATION_TO_USER_ERR_NON_USER, userId);
        }
//        2.验证组织机构合法性
        List<SysOrganizationResponsibleRO> sysOrganizationResponsibleROList = sysGrantOrganizationToUserRO.getOrganizationIdList();
        if (sysOrganizationResponsibleROList.size() <= 0) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.GRANT_ORGANIZATION_TO_USER_ERR_NON_ORGANIZATION.toString(), sysOrganizationResponsibleROList.toString());
            return Result.errorsFormat(SysTipEnum.GRANT_ORGANIZATION_TO_USER_ERR_NON_ORGANIZATION, sysOrganizationResponsibleROList.toString());
        }
        List<Long> organizationIdList = new ArrayList<>();
        for (SysOrganizationResponsibleRO sysOrganizationResponsibleRO : sysOrganizationResponsibleROList) {
            organizationIdList.add(sysOrganizationResponsibleRO.getOrganizationId());
        }
//        3.验证组织机构数量匹配
        List<SysOrganization> sysOrganizationList = serviceBySysOrganizationService.listByIds(organizationIdList);
        if (sysOrganizationList.size() != organizationIdList.size()) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.GRANT_ORGANIZATION_TO_USER_ERR_NON_ORGANIZATION.toString(), organizationIdList.toString());
            return Result.errorsFormat(SysTipEnum.GRANT_ORGANIZATION_TO_USER_ERR_NON_ORGANIZATION, organizationIdList.toString());
        }
        return Result.ok();
    }

    /**
     * 组织机构分配用户校验
     *
     * @param sysGrantUserToOrganizationRO - 组织机构分配用户
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    private Result authorizingUserToOrganizationVerification(final SysGrantUserToOrganizationRO sysGrantUserToOrganizationRO) {
//        1.验证组织机构合法性
        Long organizationId = sysGrantUserToOrganizationRO.getOrganizationId();
        SysOrganization sysOrganization = serviceBySysOrganizationService.getById(organizationId);
        if (ConvertUtils.isEmpty(sysOrganization)) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.AUTHORIZING_USER_TO_ORGANIZATION_NON_ORGANIZATION.toString(), organizationId);
            return Result.errorsFormat(SysTipEnum.AUTHORIZING_USER_TO_ORGANIZATION_NON_ORGANIZATION, organizationId);
        }
//        2.验证用户合法性
        List<SysUserResponsibleOrganizationRO> sysUserResponsibleOrganizationROList = sysGrantUserToOrganizationRO.getUserIdList();
        if (sysUserResponsibleOrganizationROList.size() <= 0) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.AUTHORIZING_USER_TO_ORGANIZATION_NON_USER.toString(), sysUserResponsibleOrganizationROList.toString());
            return Result.errorsFormat(SysTipEnum.AUTHORIZING_USER_TO_ORGANIZATION_NON_USER, sysUserResponsibleOrganizationROList.toString());
        }
        List<Long> userIdList = new ArrayList<>();
        for (SysUserResponsibleOrganizationRO sysUserResponsibleOrganizationRO : sysUserResponsibleOrganizationROList) {
            userIdList.add(sysUserResponsibleOrganizationRO.getUserId());
        }
//        3.验证用户数量匹配
        List<SysUser> sysUserList = serviceBySysUserService.listByIds(userIdList);
        if (sysUserList.size() != userIdList.size()) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.AUTHORIZING_USER_TO_ORGANIZATION_NON_USER.toString(), userIdList.toString());
            return Result.errorsFormat(SysTipEnum.AUTHORIZING_USER_TO_ORGANIZATION_NON_USER, userIdList.toString());
        }
        return Result.ok();
    }

    /**
     * 组织机构取消分配用户
     *
     * @param sysGrantUserToOrganizationRO - 组织机构分配用户
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    private Result unAuthorizingUserToOrganizationVerification(final SysGrantUserToOrganizationRO sysGrantUserToOrganizationRO) {
        return authorizingUserToOrganizationVerification(sysGrantUserToOrganizationRO);
    }

    /**
     * 分页查询组织机构
     *
     * @param pagingQueryOrganizationDTO - 组织机构查询对象
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    public Result pagingQuerySysOrganizationParentListByParameter(final PagingQueryOrganizationDTO pagingQueryOrganizationDTO) {
        try {
            Page<SysOrganizationTreeVO> sysMenuParentListByParameter = serviceBySysOrganizationService.pagingQuerySysOrganizationParentListByParameter(pagingQueryOrganizationDTO);
            return Result.ok(sysMenuParentListByParameter);
        } catch (Exception e) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.ORGANIZATION_PAGE_QUERY_ERR.toString(), e.getMessage());
            return Result.errorsFormat(SysTipEnum.ORGANIZATION_PAGE_QUERY_ERR, e.getMessage());
        }
    }

    /**
     * 查询组织机构树接口
     *
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    public Result querySysOrganizationTreeList() {
        try {
            List<SysOrganizationTreeVO> sysOrganizationTreeVOList = serviceBySysOrganizationService.querySysOrganizationTreeList();
            return Result.ok(sysOrganizationTreeVOList);
        } catch (Exception e) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.ORGANIZATION_TREE_QUERY_ERR.toString(), e.getMessage());
            return Result.errorsFormat(SysTipEnum.ORGANIZATION_TREE_QUERY_ERR, e.getMessage());
        }
    }

    /**
     * 组织机构添加
     *
     * @param sysOrganizationAddRO - 组织机构添加对象
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    public Result addOrganization(SysOrganizationAddRO sysOrganizationAddRO) {
//        1.校验组织机构名重复
        SysOrganization sysOrganization = serviceBySysOrganizationService.queryOrganizationByName(sysOrganizationAddRO.getOrganizationName());
        Result result = organizationAddVerification(sysOrganization);
        if (!result.isSuccess()) {
            return result;
        }
//        2.父级ID存在校验
        Long parentId = sysOrganizationAddRO.getParentId();
        String parentCode = CommonConstant.EMPTY;
        if (parentId != null && parentId != 0) {
            SysOrganization sysOrganizationParent = serviceBySysOrganizationService.getById(parentId);
            result = organizationAddVerificationForParent(sysOrganizationParent);
            if (!result.isSuccess()) {
                return result;
            }
            parentCode = sysOrganizationParent.getOrganizationCode();
        }
//        3.添加组织机构
        sysOrganization = SysOrganizationConverter.INSTANCE.addRo2domain(sysOrganizationAddRO);
        // 3.1生成组织机构编号
        String[] organizationCodSpecimen = generateOrganizationCode(parentCode, parentId);
        if (organizationCodSpecimen.length >= 1) {
            sysOrganization.setOrganizationCode(organizationCodSpecimen[organizationCodSpecimen.length - 1]);
            if (serviceBySysOrganizationService.save(sysOrganization)) {
                addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ORGANIZATION_ADD_OK.toString(), sysOrganization.getOrganizationName());
                result.setResult(SysOrganizationConverter.INSTANCE.po2Vo(sysOrganization));
                return result.success();
            }
        }
//        添加失败
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ORGANIZATION_ADD_ERR.toString(), sysOrganizationAddRO.getOrganizationName());
        return result.errorFormat(SysTipEnum.ORGANIZATION_ADD_ERR, sysOrganizationAddRO.getOrganizationName());
    }

    /**
     * 组织机构删除
     *
     * @param organizationIdentification - 组织机构标识
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    public Result deleteOrganization(final Long organizationIdentification) {
//        1.删除组织机构校验
        SysOrganization sysOrganization = serviceBySysOrganizationService.getById(organizationIdentification);
        Result result = organizationDeleteVerification(sysOrganization);
        if (!result.isSuccess()) {
            return result;
        }
//        2.删除组织机构
        boolean delete = serviceBySysOrganizationService.deleteOrganizationById(organizationIdentification);
        if (!delete) {
//            删除失败
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.ORGANIZATION_DELETE_ERR.toString(), sysOrganization.getOrganizationName());
            return result.errorFormat(SysTipEnum.ORGANIZATION_DELETE_ERR, sysOrganization.getOrganizationName());
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.ORGANIZATION_DELETE_OK.toString(), sysOrganization.getOrganizationName());
        return Result.ok();
    }

    /**
     * 组织机构查看
     *
     * @param organizationIdentification - 组织机构标识
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    public Result queryOrganizationById(final Long organizationIdentification) {
        SysOrganization sysOrganization = serviceBySysOrganizationService.getById(organizationIdentification);
        if (ConvertUtils.isEmpty(sysOrganization)) {
//            1.根据组织机构信息查询，该组织机构不存在
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.MENU_TREE_QUERY_ERR.toString());
            return Result.errorsFormat(SysTipEnum.MENU_TREE_QUERY_ERR);
        }
        return Result.ok(SysOrganizationConverter.INSTANCE.po2Vo(sysOrganization));
    }

    /**
     * 组织机构更新
     *
     * @param sysOrganizationUpdateRO - 组织机构更新对象
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    public Result updateOrganization(SysOrganizationUpdateRO sysOrganizationUpdateRO) {
//        1.更新组织机构校验
        SysOrganization sysOrganization = serviceBySysOrganizationService.getById(sysOrganizationUpdateRO.getId());
        Result result = organizationUpdateVerification(sysOrganization);
        if (!result.isSuccess()) {
            return result;
        }
//        2.组织机构名重复校验
        if (!sysOrganization.getOrganizationName().equals(sysOrganizationUpdateRO.getOrganizationName())) {
            SysOrganization sysOrganizationByName = serviceBySysOrganizationService.queryOrganizationByName(sysOrganizationUpdateRO.getOrganizationName());
            if (sysOrganizationByName != null) {
                addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ORGANIZATION_UPDATE_ERR_REPEAT.toString(), sysOrganizationByName.getOrganizationName());
                return result.errorFormat(SysTipEnum.ORGANIZATION_UPDATE_ERR_REPEAT, sysOrganizationByName.getOrganizationName());
            }
        }
//        3.组织机构更新
        boolean update = serviceBySysOrganizationService.updateByOrganizationIdentification(sysOrganizationUpdateRO);
        if (update) {
            return Result.ok();
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ORGANIZATION_UPDATE_ERR.toString(), sysOrganizationUpdateRO.getOrganizationName());
        return result.errorFormat(SysTipEnum.ORGANIZATION_UPDATE_ERR, sysOrganizationUpdateRO.getOrganizationName());
    }

    /**
     * 根据父组织机构ID查询组织机构树接口
     *
     * @param parentId - 父组织机构ID
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    public Result queryOrganizationTreeByParentId(final Long parentId) {
//        父级组织机构校验
        SysOrganization parentSysOrganization = serviceBySysOrganizationService.getById(parentId);
        if (ConvertUtils.isNotEmpty(parentSysOrganization)) {
            try {
//                查询组织机构树
                List<SysOrganizationTreeVO> sysOrganizationTreeVOList = serviceBySysOrganizationService.queryOrganizationTreeByParent(parentSysOrganization);
                return Result.ok(sysOrganizationTreeVOList);
            } catch (Exception e) {
                addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.ORGANIZATION_TREE_QUERY_ERR.toString(), e.getMessage());
                return Result.errorsFormat(SysTipEnum.ORGANIZATION_TREE_QUERY_ERR, e.getMessage());
            }
        }
        return Result.errorsFormat(SysTipEnum.ORGANIZATION_TREE_QUERY_ERR_NON_PARENT, parentId);
    }

    /**
     * 根据用户ID查询用户拥有的组织机构树接口
     *
     * @param userIdentification - 用户唯一标识
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    public Result queryOrganizationByUserIdentification(final Long userIdentification) {
//        1.用户合法性判断
        SysUser sysUser = serviceBySysUserService.getById(userIdentification);
        if (ConvertUtils.isEmpty(sysUser)) {
            return Result.errorsFormat(SysTipEnum.ORGANIZATION_TREE_QUERY_ERR_NON_USER, userIdentification);
        }
        try {
//            2.查询用户组织机构
            List<SysUserOrganizationVO> sysUserOrganizationVOList = serviceBySysOrganizationService.queryOrganizationByUserIdentification(sysUser.getId());
            return Result.ok(sysUserOrganizationVOList);
        } catch (Exception e) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.ORGANIZATION_TREE_QUERY_ERR.toString(), e.getMessage());
            return Result.errorsFormat(SysTipEnum.ORGANIZATION_TREE_QUERY_ERR, e.getMessage());
        }
    }

    /**
     * 用户授权组织机构
     *
     * @param sysGrantOrganizationToUserRO - 用户授权组织机构
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    public Result grantOrganizationToUser(SysGrantOrganizationToUserRO sysGrantOrganizationToUserRO) {
//        1.为用户授权组织机构校验
        Result result = grantOrganizationToUserVerification(sysGrantOrganizationToUserRO);
        if (result.isSuccess()) {
//            2.授权操作
            boolean grant = serviceBySysOrganizationService.grantOrganizationToUser(sysGrantOrganizationToUserRO);
            if (grant) {
                addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.GRANT_ORGANIZATION_TO_USER_OK.toString(), sysGrantOrganizationToUserRO.getUserId());
                return Result.ok();
            }
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.GRANT_ORGANIZATION_TO_USER_ERR.toString(), sysGrantOrganizationToUserRO.getUserId());
        return Result.errorsFormat(SysTipEnum.GRANT_ORGANIZATION_TO_USER_ERR, sysGrantOrganizationToUserRO.getUserId());
    }

    /**
     * 取消用户组织机构授权
     *
     * @param userIdentification - 用户唯一标识
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/17 14:54
     */
    public Result terminateAllOrganizationToUser(final Long userIdentification) {
//        1.验证用户存在
        SysUser sysUser = serviceBySysUserService.getById(userIdentification);
        if (ConvertUtils.isEmpty(sysUser)) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.TERMINATE_ALL_ORGANIZATION_TO_USER_ERR_NON_USER.toString(), userIdentification);
            return Result.errorsFormat(SysTipEnum.TERMINATE_ALL_ORGANIZATION_TO_USER_ERR_NON_USER, userIdentification);
        }
//        2.撤销授权
        boolean terminate = serviceBySysOrganizationService.terminateAllOrganizationToUser(sysUser.getId());
        if (terminate) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.TERMINATE_ALL_ORGANIZATION_TO_USER_OK.toString(), sysUser.getUsername());
            return Result.ok();
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.TERMINATE_ALL_ORGANIZATION_TO_USER_ERR.toString(), sysUser.getUsername());
        return Result.errorsFormat(SysTipEnum.TERMINATE_ALL_ORGANIZATION_TO_USER_ERR, sysUser.getUsername());
    }

    /**
     * 组织机构分配用户
     * @param sysGrantUserToOrganizationRO - 组织机构分配用户
     * @return Result -
     * @author XinLau
     * @since 2020/7/20 9:18
     * @creed The only constant is change ! ! !
     */
    public Result authorizingUserToOrganization(final SysGrantUserToOrganizationRO sysGrantUserToOrganizationRO) {
//        1.组织机构分配用户校验
        Result result = authorizingUserToOrganizationVerification(sysGrantUserToOrganizationRO);
        if (result.isSuccess()) {
//            2.授权操作
            boolean grant = sysUserOrganizationService.authorizingUserToOrganization(sysGrantUserToOrganizationRO);
            if (grant) {
                addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.AUTHORIZING_USER_TO_ORGANIZATION_OK.toString(), sysGrantUserToOrganizationRO.getOrganizationId());
                return Result.ok();
            }
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.AUTHORIZING_USER_TO_ORGANIZATION_ERR.toString(), sysGrantUserToOrganizationRO.getOrganizationId());
        return Result.errorsFormat(SysTipEnum.AUTHORIZING_USER_TO_ORGANIZATION_ERR, sysGrantUserToOrganizationRO.getOrganizationId());
    }

    /**
     * 组织机构取消分配用户
     * @param sysGrantUserToOrganizationRO - 组织机构分配用户
     * @return Result -
     * @author XinLau
     * @since 2020/7/20 9:18
     * @creed The only constant is change ! ! !
     */
    public Result unAuthorizingUserToOrganization(final SysGrantUserToOrganizationRO sysGrantUserToOrganizationRO) {
//        1.组织机构取消分配用户
        Result result = unAuthorizingUserToOrganizationVerification(sysGrantUserToOrganizationRO);
        if (result.isSuccess()) {
//            2.取消授权操作
            boolean grant = sysUserOrganizationService.unAuthorizingUserToOrganization(sysGrantUserToOrganizationRO);
            if (grant) {
                addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.UN_AUTHORIZING_USER_TO_ORGANIZATION_OK.toString(), sysGrantUserToOrganizationRO.getOrganizationId());
                return Result.ok();
            }
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.UN_AUTHORIZING_USER_TO_ORGANIZATION_ERR.toString(), sysGrantUserToOrganizationRO.getOrganizationId());
        return Result.errorsFormat(SysTipEnum.UN_AUTHORIZING_USER_TO_ORGANIZATION_ERR, sysGrantUserToOrganizationRO.getOrganizationId());
    }

}